package com.ruoyi.system.api;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.date.DateUtils;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
public class GZFileExtractor {

	public static void ioOfHutool() {
		String fileName = "F:\\gz\\2023102417";
		final String CHARSET_NAME = "UTF-8";

		List<String> content = new ArrayList<>();

		try {
			IoUtil.readLines(new FileInputStream(fileName), CharsetUtil.charset(CHARSET_NAME), content);
		} catch (Exception e) {
			e.printStackTrace();
		}

		JSONArray jsonArr = new JSONArray();
		content.forEach(str ->{
			JSONObject parseObject = JSONObject.parseObject(str);
			jsonArr.add(parseObject);
		});
		jsonArr.forEach(json ->{
			System.out.println("发送者："+JSONObject.parseObject(json.toString()).getJSONObject("payload").getJSONObject("ext").getJSONObject("ziji").getString("nickName"));
			System.out.println("接受者："+JSONObject.parseObject(json.toString()).getJSONObject("payload").getJSONObject("ext").getJSONObject("add").getString("nickName"));
			System.out.println("消息内容："+JSONObject.parseObject(json.toString()).getJSONObject("payload").getJSONArray("bodies").getJSONObject(0).getString("msg"));
			System.out.println("消息类型："+JSONObject.parseObject(json.toString()).getJSONObject("payload").getJSONArray("bodies").getJSONObject(0).getString("type"));
		});
		System.out.println(content.size());
	}
	public static void main(String[] args) {
		//fileOfHutool();
		//ioOfHutool();
	}

	public static List<ChatRecordDTO> fileOfHutool(String fileName) {
		final String CHARSET_NAME = "UTF-8";
		List<String> content = FileUtil.readLines(fileName, CHARSET_NAME);
		System.out.println("内容打印：");
		System.out.println(content.toString());
		List<ChatRecordDTO> chatList = new ArrayList<ChatRecordDTO>();
		content.forEach(str ->{
			ChatRecordDTO chat = new ChatRecordDTO();
			JSONObject parseObject = JSONObject.parseObject(str);
			JSONObject payload = parseObject.getJSONObject("payload");
			JSONObject ext = payload.getJSONObject("ext");
			JSONObject jsonObject = payload.getJSONArray("bodies").getJSONObject(0);
			JSONObject add = ext.getJSONObject("add");
			if(null != add) {
				chat.setFromName(add.getString("nickName"));
				chat.setFromHeadPic(add.getString("headPic"));
			}
			JSONObject ziji = ext.getJSONObject("ziji");
			if(null != ziji) {
				chat.setToName(ziji.getString("nickName"));
				chat.setToHeadPic(ziji.getString("headPic"));
			}
			chat.setContent(jsonObject.getString("msg"));
			chat.setMsgType(jsonObject.getString("type"));
			if(chat.getContent().contains("png") || chat.getContent().contains("jpg")) {
				chat.setMsgType("img");
			}
			long longValue = parseObject.getLongValue("timestamp");
			if(longValue > 0) {
				chat.setTime(DateUtils.timestampToString(longValue));
			}
			chatList.add(chat);
		});
		return chatList;
		//        jsonArr.forEach(json ->{
		//        	System.out.println("接受者："+JSONObject.parseObject(json.toString()).getJSONObject("payload").getJSONObject("ext").getJSONObject("ziji").getString("nickName"));
		//        	System.out.println("发送者："+JSONObject.parseObject(json.toString()).getJSONObject("payload").getJSONObject("ext").getJSONObject("add").getString("nickName"));
		//        	System.out.println("消息内容："+JSONObject.parseObject(json.toString()).getJSONObject("payload").getJSONArray("bodies").getJSONObject(0).getString("msg"));
		//        	System.out.println("消息类型："+JSONObject.parseObject(json.toString()).getJSONObject("payload").getJSONArray("bodies").getJSONObject(0).getString("type"));
		//        });
	}
	public static void main1(String[] args) {
		String gzFilePath = "http://ebs-chatmessage-a1.easemob.com/history/7D/1151230809175214/demo/2023102417.gz?Expires=1698215966&OSSAccessKeyId=LTAI4Fy3Zj6ha5XFZEMdRidM&Signature=LgzBlu%2BgD9kR8qd2qchVKpknS%2B4%3D";
		String outputFilePath = "F:\\gz";

		try {
			//建立gzip压缩文件输入流
			FileInputStream fin = new FileInputStream(gzFilePath);
			//建立gzip解压工作流
			GZIPInputStream gzin = new GZIPInputStream(fin);
			//建立解压文件输出流
			outputFilePath = gzFilePath.substring(0,gzFilePath.lastIndexOf('.'));
			//            ouputfile = ouputfile.substring(0,ouputfile.lastIndexOf('.'));
			FileOutputStream fout = new FileOutputStream(outputFilePath);
			int num;
			byte[] buf=new byte[1024];

			while ((num = gzin.read(buf,0,buf.length)) != -1)
			{
				fout.write(buf,0,num);
			}
			gzin.close();
			fout.close();
			fin.close();

			System.out.println("解压完成！");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
